package com.codingfly.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.codingfly.core.jdbc.service.impl.JPAServiceImpl;
import com.codingfly.core.util.TreeListUtils;
import com.codingfly.entity.SysDict;
import com.codingfly.entity.SysDictItem;
import com.codingfly.model.vo.DictItemVo;
import com.codingfly.repository.SysDictItemRepository;
import com.codingfly.repository.SysDictRepository;
import com.codingfly.service.SysDictService;
import com.github.wenhao.jpa.Specifications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SysDictServiceImpl extends JPAServiceImpl<SysDictRepository, SysDict, Long> implements SysDictService {
    @Autowired
    private SysDictRepository sysDictRepository;
    @Autowired
    private SysDictItemRepository sysDictItemRepository;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate namedJdbcTemplate;

    @Override
    public SysDict findByCode(String code) {
        Optional<SysDict> optional = sysDictRepository.findOne(Specifications.<SysDict>and().eq("code", code).build());
        return optional.isPresent()? optional.get():null;
    }

    @Override
    public List<DictItemVo> dictItemTree(Long id) {
        List<SysDictItem> list = sysDictItemRepository.findAll(Specifications.<SysDictItem>and().eq("dictId", id).build());
        List<DictItemVo> voList = BeanUtil.copyToList(list, DictItemVo.class);
        return TreeListUtils.buildAscTreeList(voList, "id", "parent_id", "children", "order_no");
    }

    @Override
    public List<DictItemVo> dictItemTreeByCode(String code) {
        SysDict sysDict = findByCode(code);
        return sysDict==null? new ArrayList():dictItemTree(sysDict.getId());
    }

    @Override
    public Map<String, List<DictItemVo>> manyDictItemTree(Collection<String> dictCodes) {
        List<SysDict> sysDicts = sysDictRepository.findAll(Specifications.<SysDict>and().in("code", dictCodes).build());
        if (ObjectUtils.isEmpty(sysDicts)) {
            return new HashMap();
        }
        List<Long> sysDictIds = sysDicts.stream().map(item->item.getId()).collect(Collectors.toList());
        Map<Long, String> map = sysDicts.stream().filter(item-> StringUtils.isEmpty(item.getCode())).collect(Collectors.toMap(item->item.getId(), item->item.getCode()));
        List<SysDictItem> list = sysDictItemRepository.findAll(Specifications.<SysDictItem>and().in("dictId", sysDictIds).build());
        Map<Long, List<SysDictItem>> mapList = list.stream().filter(item->item.getDictId()!=null).collect(Collectors.groupingBy(SysDictItem::getDictId));
        Map<String, List<DictItemVo>> returnMap = new HashMap();
        for (Long key:mapList.keySet()) {
            List<SysDictItem> itemList = mapList.get(key);
            List<DictItemVo> voList = BeanUtil.copyToList(itemList, DictItemVo.class);
            List<DictItemVo> treeList = TreeListUtils.buildAscTreeList(voList, "id", "parent_id", "children", "order_no");
            returnMap.put(map.get(key), treeList);
        }
        return returnMap;
    }

}
